#!/bin/bash
# Author:               liuyl
# Version:              1.0
# Mail:                 liuyonglu@linkstec.***
# Date:                 2021-9-06
# Description:          部署、启动、停止、强制停止、重启SpringBootAdmin服务

cd /usr/local/SpringBootAdmin-1.0.0/

STDOUT_FILE=/dev/null

export JAVA_HOME=/usr/java/
export PATH=$PATH:${JAVA_HOME}/bin

JAVA_OPTS=" -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Djava.ext.dirs=./lib "

JAVA_JVM_OPTS=""
BITS=`java -version 2>&1 | grep -i 64-bit`
if [ -n "$BITS" ]; then
    JAVA_JVM_OPTS=" -server -Xms512m -Xmx512m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled  -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 "
else
    JAVA_JVM_OPTS=" -server -Xms512m -Xmx512m -XX:PermSize=64m -XX:SurvivorRatio=2 -XX:+UseParallelGC "
fi

JAVA_DEBUG_OPTS=""
if [ "$1" = "debug" ]; then
    JAVA_DEBUG_OPTS=" -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n "
fi

JAVA_JMX_OPTS=""
if [ "$1" = "jmx" ]; then
    JAVA_JMX_OPTS=" -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false "
fi

echo "JAVA_OPTS=$JAVA_OPTS"
echo "JAVA_JVM_OPTS=$JAVA_JVM_OPTS"
echo "JAVA_DEBUG_OPTS=$JAVA_DEBUG_OPTS"

case "$1" in

  start)
  if [ ! -f "SpringBootAdmin.pid" ]; then
    echo "if you start failed,try this >>>>>>>>>>> java $JAVA_OPTS $JAVA_JVM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS com.linkstec.AdminServerApplication"
    nohup java $JAVA_OPTS $JAVA_JVM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS com.linkstec.AdminServerApplication >/dev/null 2>&1 &
    echo $! > SpringBootAdmin.pid
    echo '开始启动监控服务'
	echo `cat SpringBootAdmin.pid`
    
  else
    echo '监控服务已经启动'
  fi
  ;;

  stop)
    kill `cat SpringBootAdmin.pid`
    rm -rf SpringBootAdmin.pid
    echo '停止监控服务'
    ;;

  forcestop)
    kill -9 `cat SpringBootAdmin.pid`
    rm -rf SpringBootAdmin.pid
    echo '停止监控服务'
    ;;

  restart)
    $0 stop
    sleep 1
    $0 start
    echo '重启监控服务'
    ;;

  status)
  if [ -f "SpringBootAdmin.pid" ]; then
    echo '运行中'
  else
    echo '已停止'
  fi
  ;;

  *)
    echo "Usage: run.sh {start|stop|restart|status}"
    ;;


esac

exit 0
